from PySide6.QtWidgets import QWidget, QPushButton
from PySide6.QtCore import Signal, Slot, Property, Qt, QObject
from PySide6.QtGui import QCloseEvent
import Global

class MyWidget(QWidget):
    """自定义子窗口类"""
    myPropertyChanged = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.m_my_property = 0
        self.init_ui()

    def init_ui(self):
        """初始化子窗口UI"""
        self.setWindowTitle("子窗口")
        self.resize(300, 200)
        
        # 可以在这里添加子窗口特有的控件
        # 示例：添加一个测试按钮
        self.test_btn = QPushButton("调用子窗口方法", self)
        self.test_btn.setGeometry(80, 100, 140, 30)
        self.test_btn.clicked.connect(self.slot_fun)

    # 属性相关方法
    def get_my_property(self):
        return self.m_my_property

    def set_my_property(self, new_value):
        if self.m_my_property != new_value:
            self.m_my_property = new_value
            self.myPropertyChanged.emit()

    myProperty = Property(int, get_my_property, set_my_property, notify=myPropertyChanged)

    # 元对象可调用方法
    def meta_fun(self):
        print("调用子窗口元方法: meta_fun")

    # 槽函数
    @Slot()
    def slot_fun(self):
        print("调用子窗口槽函数: slot_fun")

    # 重写关闭事件
    def closeEvent(self, event: QCloseEvent):
        parent_widget = self.parentWidget()
        if parent_widget:
            self.hide()
            print("【子窗口】已隐藏")
            parent_widget.show()
            print("【主窗口】已显示")
        else:
            print("【警告】未找到父窗口（主窗口）")

        event.ignore()